package com.itboye.sunsun;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.CarouselPictureBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.custome.CarouselView;
import com.itboye.sunsun.dialog.WarnDialog;
import com.itboye.sunsun.login.ui.LoginActivity;
import com.itboye.sunsun.luntan.LunTanHomePageActivity;
import com.itboye.sunsun.luntan.TieZiDetailActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.news.NewsHotReadMainFragmentActivity;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsun.web.ui.WebActivity;
import com.itboye.sunsunhome.www.aq.DeviceActivity;
import com.itboye.sunsunhome.R;

public class HomeFragment extends BaseFragment implements OnClickListener {

	private View yubaomu;
	private LinearLayout luntan;
	private View shop;
	private LinearLayout linear_newsread;
	CarouselView topLunbo;
	// CarouselView bottomLunbo;
	View guanwang;
	Button shuxin;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.fragment_home, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		showProgressDialog("数据加载中，请稍后", true);
		getTopBannerData();
		// getBottomBannerData();
		shuxin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getTopBannerData();
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.yubaomu:

			final String uid = (String) SPUtils.get(getContext(), null,
					SPContants.USER_ID, "");

			boolean flag = (boolean) SPUtils.get(getContext(),
					"yubaomu_first_use", uid, true);
			if ("".equals(uid)) {
				Intent intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			} else if (flag == false) {
				App.ctx.init();
				startActivity(new Intent(getActivity(), DeviceActivity.class));
			} else {

				new WarnDialog("条款与申明", getString(R.string.yubaomu_warning),
						new WarnDialog.OnDialogSelectListener() {

							@Override
							public void onDialoSelect(boolean isAgree,
									boolean noTip) {
								if (isAgree) {
									App.ctx.init();
									startActivity(new Intent(getActivity(),
											DeviceActivity.class));

								} else {

								}
								if (noTip) {
									SPUtils.put(getContext(),
											"yubaomu_first_use", uid, false);
								}
							}
						}).show(getChildFragmentManager(), null);

			}

			break;

		case R.id.shop:
//			startActivity(new Intent(getActivity(), AllShopActivity.class));
			break;
		case R.id.luntan:
			// Intent mIntent=new Intent(getActivity(),ForumHoneActivty.class);
			// startActivity(mIntent);
			startActivity(new Intent(getContext(), LunTanHomePageActivity.class));
			break;

		case R.id.guanwang:
			Intent intent = new Intent(getContext(), WebActivity.class);
			intent.putExtra("title", "森森官网");
			intent.putExtra("url", "http://m.sunsuncn.com");
			startActivity(intent);
			break;
		case R.id.linear_newsread:
			Intent intent2 = new Intent(getContext(),
					NewsHotReadMainFragmentActivity.class);
			startActivity(intent2);
			break;
		}
	}

	private void getTopBannerData() {
		MyJsonRequest<List<CarouselPictureBean>> request = new MyJsonRequest.Builder<List<CarouselPictureBean>>()
				.apiVer("100")
				.typeKey("BY_Picture_getURL")
				.param("position", "6045")
				.requestListener(
						new XRequestListener<List<CarouselPictureBean>>() {

							@Override
							public void onResponse(
									List<CarouselPictureBean> arg0) {
								shuxin.setVisibility(View.GONE);
								try {
									closeProgressDialog();
								} catch (Exception e) {
									// TODO: handle exception
								}
								List<ImageView> bmps = new ArrayList<ImageView>();
								for (CarouselPictureBean pictureBean : arg0) {
									ImageView imageView = new ImageView(App.ctx);

									imageView
											.setScaleType(ScaleType.CENTER_CROP);
									XImageLoader.load(pictureBean.getImgUrl(),
											imageView);
									bmps.add(imageView);
									imageView.setTag(pictureBean);
									imageView
											.setOnClickListener(new OnClickListener() {

												@Override
												public void onClick(View v) {
													// TODO Auto-generated
													// method stub
													String uid = SPUtils.get(
															App.ctx, null,
															SPContants.USER_ID,
															"")
															+ "";
													CarouselPictureBean bean = (CarouselPictureBean) v
															.getTag();
													// 轮播图跳转类型 6070 链接 6071
													// app商品 6072 app帖子
													// if ("6070".equals(bean
													// .getUrlType())) {
													//
													// if (!bean.getUrl()
													// .equals("")) {
													//
													// Intent intent = new
													// Intent(
													// App.ctx,
													// WebActivity.class);
													// //
													// intent.putExtra("title",
													// // "商品详情");
													// intent.putExtra(
													// "url",
													// bean.getUrl());
													// startActivity(intent);
													// }
													// } else
													System.out.println("bean.getUrl()"+bean
															.getUrl());
													if ("http://sunsun.itboye.com/index.php/Webview/Game/index"
															.equals(bean
																	.getUrl())) {

														if (uid == null
																|| uid == "") {
															startActivity(new Intent(
																	getActivity(),
																	LoginActivity.class));
														} else {
															Intent intent = new Intent(
																	App.ctx,
																	WebActivity.class);
															intent.putExtra(
																	"title",
																	"抽奖");
															intent.putExtra(
																	"url",
																	bean.getUrl()
																			+ "?uid="
																			+ uid
																			+ "&psw="
																			+ SPUtils
																					.get(getActivity(),
																							null,
																							SPContants.PASSWORD,
																							"")
																			+ "");
															startActivity(intent);
															System.out.println("抽奖链接"
																	+ bean.getUrl()
																	+ "?uid="
																	+ uid
																	+ "&psw="
																	+ SPUtils
																			.get(getActivity(),
																					null,
																					SPContants.PASSWORD,
																					"")
																	+ "");
														}
													}
													if ("6071".equals(bean
															.getUrlType())) {
//														Intent intent = new Intent(
//																App.ctx,
//																GoodDetailActivity.class);
//														intent.putExtra("what",
//																bean.getUrl());
//														startActivity(intent);
													} else if ("6072".equals(bean
															.getUrlType())) {
														Intent intent=new Intent(
																App.ctx,
																TieZiDetailActivity.class);
														intent.putExtra("tid", bean.getUrl());
														startActivity(intent);

													}
													
													System.out.println("抽奖链接2"
															+ bean.getUrl()
															+ "?uid="
															+ uid
															+ "&psw="
															+ SPUtils
																	.get(getActivity(),
																			null,
																			SPContants.PASSWORD,
																			"")
															+ "typer"
															+ bean.getUrlType());
												}
											});
								}
								topLunbo.setImageBitmaps(bmps);
							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						shuxin.setVisibility(View.VISIBLE);
						try {
							closeProgressDialog();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
